<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['setting']=$this->plugin_setting('basic');
$worker=pdo_get('jy_farmer_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
if(!$worker){
	exi('找不到员工信息！','error','jy_farmer/myhome/');
}
$return['worker']=$worker;
if($_RQ['op']=='main' || $_RQ['op']=='wait'){
	$return['params']=array('status'=>intval($_RQ['status']),'isimg'=>1);
	$return['post']=array('jy'=>1);
	$return['list']=array();
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'isimg'=>intval($_RQ['isimg']));
	if($_RQ['number']){
		$params['number']=$_RQ['number'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_farmer_gdopt',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$month=date('m');
		$month=intval($month);
		foreach($list as &$item){
			$item['statustitle']=$this->gdopt_status[$item['status']];
			$item['animal']=pdo_get('jy_farmer_guoshu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['guoshu_id']),array('image','title','classify_id','growtime','shouyue','shoukg','shoufee'));
			$item['animal']['image']=tomedia($item['animal']['image']);
			$item['animal']['atitle']=pdo_getcolumn('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['animal']['classify_id']),'title');
			$item['animal']['shoutype']=pdo_getcolumn('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['animal']['classify_id']),'shoutype');
			$item['animal']['shoutypetitle']=$this->shoutype[$item['animal']['shoutype']];
			if($item['number']<10){
				$item['number']='000'.$item['number'];
			}elseif($item['number']<100 && $item['number']>9){
				$item['number']='00'.$item['number'];
			}else{
				$item['number']='0'.$item['number'];
			}
			$item['order']=pdo_get('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['order_id']),array('address','total','ordersn','money','createtime'));
			$item['animal']['shoukg']=$item['animal']['shoukg']*$item['order']['total'];
			$item['animal']['shoufee']=$item['animal']['shoufee']*$item['order']['total'];
			$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>0,'type'=>0,'uniacid'=>$_SESSION['uniacid']),'id');
			if($totype){
				$item['totype0']=1;
			}
			$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>1,'type'=>0,'uniacid'=>$_SESSION['uniacid']),'id');
			if($totype){
				$item['totype1']=1;
			}
			$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>2,'type'=>0,'uniacid'=>$_SESSION['uniacid']),'id');
			if($totype){
				$item['totype2']=1;
			}
			$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>3,'type'=>0,'uniacid'=>$_SESSION['uniacid']),'id');
			if($totype){
				$item['totype3']=1;
			}
			if($item['animal']['shouyue']==$month && ($item['status']==1 || $item['status']==2)){
				$item['isshou']=1;
			}
			$shouday=$item['order']['createtime']+$item['animal']['growtime']*24*3600;
			if($shouday<time() && ($item['status']==1 || $item['status']==2)){
				$item['isshou']=1;
			}
			$shouday=date('Y-m-d',$shouday);
			$item['animal']['shouday']=$shouday;
			$item['imgs']=$item['imgs']?json_decode($item['imgs'],true):array();
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['id']){
		pdo_update('jy_farmer_gdopt',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		exi('接单成功！');
	}else{
		exi('该果树不存在，请重新选择','error');
	}
}
if($_RQ['op']=='status3'){
	if($_RQ['id']){
		pdo_update('jy_farmer_gdopt',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		exi('操作成功！');
	}else{
		exi('该果树不存在，请重新选择','error');
	}
}
if($_RQ['op']=='status4'){
	if($_RQ['id']){
		if(!$_RQ['shoukg']){
			exi('请填写收获重量，单位KG！','error');
		}
		pdo_update('jy_farmer_gdopt',array('status'=>4,'shoukg'=>$_RQ['shoukg']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		exi('操作成功！');
	}else{
		exi('该果树不存在，请重新选择','error');
	}
}
if($_RQ['op']=='adopt'){
	if($_RQ['id']){
		$status=pdo_getcolumn('jy_farmer_gdopt',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'status');
		if($status==1){
			pdo_update('jy_farmer_gdopt',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		}
		$orderid=pdo_getcolumn('jy_farmer_gdopt',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'order_id');
		$data=array(
			'order_id'=>$orderid,
			'pid'=>$_RQ['id'],
			'msg'=>'浇水一次',
			'totype'=>0,
			'type'=>0,
			'status'=>1,
		);
		$this->model('worker')->uplog($data,$worker['id']);
		exi('浇水成功！');
	}else{
		exi('该果树不存在，请重新选择','error');
	}
}
if($_RQ['op']=='weed'){
	$orderid=pdo_getcolumn('jy_farmer_gdopt',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'order_id');
	$data=array(
			'order_id'=>$orderid,
			'pid'=>$_RQ['id'],
			'msg'=>'除虫一次',
			'totype'=>1,
			'type'=>0,
			'status'=>1,
	);
	$this->model('worker')->uplog($data,$worker['id']);
	exi('除虫完成');	
}
if($_RQ['op']=='worm'){
	$orderid=pdo_getcolumn('jy_farmer_gdopt',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'order_id');
	$data=array(
			'order_id'=>$orderid,
			'pid'=>$_RQ['id'],
			'msg'=>'除草一次',
			'totype'=>2,
			'type'=>0,
			'status'=>1,
	);
	$this->model('worker')->uplog($data,$worker['id']);
	exi('除草完成');	
}
if($_RQ['op']=='shifei'){
	$orderid=pdo_getcolumn('jy_farmer_gdopt',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'order_id');
	$data=array(
			'order_id'=>$orderid,
			'pid'=>$_RQ['id'],
			'msg'=>'施肥一次',
			'totype'=>3,
			'type'=>0,
			'status'=>1,
	);
	$this->model('worker')->uplog($data,$worker['id']);
	exi('施肥完成');
}
if($_RQ['op']=='upimage'){
	$postdata=get_postdata();
	$log_id=intval($postdata['log_id']);
	if(!$log_id){
		exi('订单不存在，请重新选择订单','error');
	}
	if(count($postdata['images'])<1){
		exi('图片必须1张以上！','error');
	}
	pdo_update('jy_farmer_gdopt',array('isimg'=>1,'imgs'=>ijson_encode($postdata['images'])),array('uniacid'=>$_SESSION['uniacid'],'id'=>$log_id));
	$this->model('notice')->upguoshuimg($log_id);
	exi('上传成功！','/worker_guoshu/');
}
